package fr.vahren.sriksal.generation;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

public class DrawingUtils {

	private static DrawingUtils singleton;

	// private JFrame frame;
	private BufferedImage image;

	public static DrawingUtils getInstance() {
		if (singleton == null) {
			singleton = new DrawingUtils();
		}
		return singleton;
	}

	public DrawingUtils() {
		// // Create a JFrame
		// frame = new JFrame("Erosion");
		// frame.setSize(600, 600);
		// frame.setVisible(true);
		// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
	}

	public void drawAll(int[][] waterMap, int[][] map, int[][] terrainMap,
			int[][] biomeMap) {
		int color, red, green, blue;
		// grey scale altitude
		for (int i = 0; i < waterMap.length; i++) {
			for (int j = 0; j < waterMap[i].length; j++) {
				if (waterMap[i][j] == 0 && biomeMap[i][j] == 0) {
					// draw terrain (earth/stone)
					red = terrainMap[i][j] > 0 ? 0 : map[i][j];
					green = terrainMap[i][j] > 0 ? map[i][j] : 0;
					blue = 0;
				} else if (biomeMap[i][j] == Eroder.WOOD) {
					red = 0;
					green = 150;
					blue = 100;
				} else if (biomeMap[i][j] == Eroder.SAND) {
					red = waterMap[i][j] == 0 ? 255 : 0;
					green = waterMap[i][j] == 0 ? 255 : waterMap[i][j] / 2;
					blue = waterMap[i][j];
				} else {
					red = 0;
					green = 0;
					blue = waterMap[i][j];
				}
				color = ((red << 8) + green << 8) + blue;
				image.setRGB(i, j, color);
			}
		}

		// draw in the JFrame
		// frame.getGraphics().drawImage(image, 50, 50, null);
	}

	public void drawRivers(int[][] map, List<Position> sources,
			List<List<Position>> paths) {
		int color, red, green, blue;
		// grey scale altitude
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				red = map[i][j];
				green = map[i][j];
				blue = map[i][j];
				color = ((red << 8) + green << 8) + blue;
				image.setRGB(i, j, color);
			}
		}
		for (Position source : sources) {
			image.setRGB(source.getX(), source.getY(), 255);
		}
		for (List<Position> path : paths) {
			for (Position step : path) {
				image.setRGB(step.getX(), step.getY(), 200);
			}
		}

		// draw in the JFrame
		// frame.getGraphics().drawImage(image, 50, 50, null);
	}

	public void drawMap(int[][] map, int[][] terrainMap, List<Drop> drops,
			int factor) {

		int color, red, green, blue;
		// grey scale altitude
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				red = terrainMap[i][j] > 0 ? 0 : map[i][j] * factor;
				green = terrainMap[i][j] > 0 ? map[i][j] * factor : 0;
				blue = 0;
				color = ((red << 8) + green << 8) + blue;
				image.setRGB(i, j, color);
			}
		}
		for (Drop drop : drops) {
			image.setRGB(drop.getPosition().getX(), drop.getPosition().getY(),
					255 << 8 << 8);
		}

		// draw in the JFrame
		// frame.getGraphics().drawImage(image, 50, 50, null);
	}

	public void exportAll(int[][] waterMap, int[][] map, int[][] terrainMap,
			int[][] biomeMap, String fileName) {
		// fill BufferedImage
		drawAll(waterMap, map, terrainMap, biomeMap);
		File file = new File(fileName);
		try {
			ImageIO.write(image, "bmp", file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
